const Router = require('@koa/router');
const { checkAuth, checkPermission } = require('../middlewares/auth');
const applicationController = require('../controllers/application.controller');

const router = new Router({ prefix: '/api/applications' });

/**
 * @swagger
 * /api/applications:
 *   post:
 *     tags: [应用管理]
 *     summary: 创建应用
 *     description: 创建新的应用配置
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - type
 *             properties:
 *               name:
 *                 type: string
 *                 description: 应用名称
 *                 example: "校园导航"
 *               type:
 *                 type: string
 *                 enum: [app, web, miniprogram]
 *                 description: 应用类型（原生应用/网页/小程序）
 *                 example: "miniprogram"
 *               version:
 *                 type: string
 *                 description: 应用版本号
 *                 example: "1.0.0"
 *               config:
 *                 type: object
 *                 description: 应用配置信息
 *               description:
 *                 type: string
 *                 description: 应用描述
 *                 example: "校园导航与服务应用"
 *               releaseNotes:
 *                 type: string
 *                 description: 版本更新说明
 *                 example: "首次发布"
 *               downloadUrl:
 *                 type: string
 *                 description: 下载地址
 *                 example: "https://example.com/download"
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "app_001"
 *                 message:
 *                   type: string
 *                   example: "应用创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.post('/', checkAuth, checkPermission('application:create'), applicationController.createApplication);

/**
 * @swagger
 * /api/applications:
 *   get:
 *     tags: [应用管理]
 *     summary: 获取应用列表
 *     description: 获取所有应用的配置信息（支持分页和筛选）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *           enum: [app, web, miniprogram]
 *         description: 应用类型筛选
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [active, inactive, maintenance]
 *         description: 应用状态筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 10
 *                 pages:
 *                   type: integer
 *                   example: 1
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 applications:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "app_001"
 *                       name:
 *                         type: string
 *                         example: "校园导航"
 *                       type:
 *                         type: string
 *                         example: "miniprogram"
 *                       version:
 *                         type: string
 *                         example: "1.0.0"
 *                       status:
 *                         type: string
 *                         example: "active"
 *                       createdAt:
 *                         type: string
 *                         format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 */
router.get('/', checkAuth, checkPermission('application:read'), applicationController.getApplications);

/**
 * @swagger
 * /api/applications/{id}:
 *   get:
 *     tags: [应用管理]
 *     summary: 获取应用详情
 *     description: 获取指定应用的详细配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 应用ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "app_001"
 *                 name:
 *                   type: string
 *                   example: "校园导航"
 *                 type:
 *                   type: string
 *                   example: "miniprogram"
 *                 version:
 *                   type: string
 *                   example: "1.0.0"
 *                 config:
 *                   type: object
 *                   description: 应用配置信息
 *                 description:
 *                   type: string
 *                   example: "校园导航与服务应用"
 *                 releaseNotes:
 *                   type: string
 *                   example: "首次发布"
 *                 downloadUrl:
 *                   type: string
 *                   example: "https://example.com/download"
 *                 status:
 *                   type: string
 *                   example: "active"
 *                 mapServices:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "map_001"
 *                       name:
 *                         type: string
 *                         example: "校园地图服务"
 *                 createdAt:
 *                   type: string
 *                   format: date-time
 *                 updatedAt:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 应用不存在
 */
router.get('/:id', checkAuth, checkPermission('application:read'), applicationController.getApplication);

/**
 * @swagger
 * /api/applications/{id}:
 *   put:
 *     tags: [应用管理]
 *     summary: 更新应用
 *     description: 更新指定应用的配置信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 应用ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 应用名称
 *                 example: "校园导航"
 *               type:
 *                 type: string
 *                 enum: [app, web, miniprogram]
 *                 description: 应用类型
 *                 example: "miniprogram"
 *               version:
 *                 type: string
 *                 description: 应用版本号
 *                 example: "1.0.1"
 *               config:
 *                 type: object
 *                 description: 应用配置信息
 *               description:
 *                 type: string
 *                 description: 应用描述
 *                 example: "校园导航与服务应用"
 *               releaseNotes:
 *                 type: string
 *                 description: 版本更新说明
 *                 example: "修复已知问题"
 *               downloadUrl:
 *                 type: string
 *                 description: 下载地址
 *                 example: "https://example.com/download"
 *               status:
 *                 type: string
 *                 enum: [active, inactive, maintenance]
 *                 description: 应用状态
 *                 example: "active"
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "应用更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 应用不存在
 */
router.put('/:id', checkAuth, checkPermission('application:update'), applicationController.updateApplication);

/**
 * @swagger
 * /api/applications/{id}:
 *   delete:
 *     tags: [应用管理]
 *     summary: 删除应用
 *     description: 删除指定的应用配置
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 应用ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "应用删除成功"
 *       400:
 *         description: 无法删除（应用正在使用中）
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 应用不存在
 */
router.delete('/:id', checkAuth, checkPermission('application:delete'), applicationController.deleteApplication);

/**
 * @swagger
 * /api/applications/{id}/map-services:
 *   post:
 *     tags: [应用管理]
 *     summary: 分配地图服务
 *     description: 为应用分配地图服务
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 应用ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - mapServiceId
 *             properties:
 *               mapServiceId:
 *                 type: string
 *                 description: 地图服务ID
 *                 example: "map_001"
 *     responses:
 *       200:
 *         description: 分配成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "地图服务分配成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 应用或地图服务不存在
 */
router.post('/:id/map-services', checkAuth, checkPermission('application:update'), applicationController.assignMapService);

/**
 * @swagger
 * /api/applications/{id}/map-services/{mapServiceId}:
 *   delete:
 *     tags: [应用管理]
 *     summary: 移除地图服务
 *     description: 从应用中移除指定的地图服务
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 应用ID
 *       - in: path
 *         name: mapServiceId
 *         required: true
 *         schema:
 *           type: string
 *         description: 地图服务ID
 *     responses:
 *       200:
 *         description: 移除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "地图服务移除成功"
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 应用或地图服务不存在
 */
router.delete('/:id/map-services/:mapServiceId', checkAuth, checkPermission('application:update'), applicationController.removeMapService);

/**
 * @swagger
 * /api/applications/{id}/status:
 *   patch:
 *     tags: [应用管理]
 *     summary: 更新应用状态
 *     description: 更新指定应用的状态
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 应用ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [active, inactive, maintenance]
 *                 description: 新的状态（活跃/停用/维护）
 *                 example: "active"
 *     responses:
 *       200:
 *         description: 状态更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "应用状态更新成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       403:
 *         description: 无权限访问
 *       404:
 *         description: 应用不存在
 */
router.patch('/:id/status', checkAuth, checkPermission('application:update'), applicationController.updateApplicationStatus);

module.exports = router; 